#!/usr/bin/env bash
export PHP_INI='/tools/php/php.ini'
export HTTPD_CONF="/tools/httpd/conf/httpd.conf"

backup_file ${PHP_INI} ${HTTPD_CONF}

# Set date.timezone. required by PHP-5.3.
perl -pi -e 's#^;(date.timezone).*#${1} = PRC#' ${PHP_INI}

#ECHO_DEBUG "Setting error_reporting to 'E_ERROR': ${PHP_INI}."
perl -pi -e 's#^(error_reporting.*=)#${1} E_ERROR;#' ${PHP_INI}

#ECHO_DEBUG "Disable several functions: ${PHP_INI}."
#perl -pi -e 's#^(disable_functions.*=)(.*)#${1}show_source,system,shell_exec,passthru,exec,phpinfo,proc_open ; ${2}#' ${PHP_INI}

#ECHO_DEBUG "Hide PHP Version in Apache from remote users requests: ${PHP_INI}."
perl -pi -e 's#^(expose_php.*=)#${1} Off;#' ${PHP_INI}

#ECHO_DEBUG "Increase 'memory_limit' to 128M: ${PHP_INI}."
perl -pi -e 's#^(memory_limit = )#${1} 128M;#' ${PHP_INI}

#ECHO_DEBUG "Increase 'upload_max_filesize', 'post_max_size' to 10/12M: ${PHP_INI}."
perl -pi -e 's/^(upload_max_filesize.*=)/${1}10M; #/' ${PHP_INI}
perl -pi -e 's/^(post_max_size.*=)/${1}12M; #/' ${PHP_INI}

# Change 'Deny from all' to 'Allow from all'.
#sed -i '.iredmailtmp' '/Each directory to/,/Note that from/s#Deny\ from\ all#Allow\ from\ all#' ${HTTPD_CONF}
#rm -f ${HTTPD_CONF}.iredmailtmp >/dev/null 2>&1

# Disable unique_id_module.
#perl -pi -e 's/^(LoadModule.*unique_id_module.*)/#${1}/' ${HTTPD_CONF}

# Add index.php in DirectoryIndex.
perl -pi -e 's#(.*DirectoryIndex.*)(index.html)#${1} index.php ${2}#' ${HTTPD_CONF}

# Add php file type.
echo 'AddType application/x-httpd-php .php' >> ${HTTPD_CONF}

/tools/httpd/bin/apachectl stop
/tools/httpd/bin/apachectl start

cp ${ROOTDIR}/index.php /tools/httpd/htdocs/
